//
//  BarViewController.swift
//  iPhone
//
//  Created by Toj on 6/13/22.
//

import UIKit
import TTCOM_iOS


class BarViewController: STBaseViewController {
    
    override var pagePath: String {
        return "startimes://player"
    }
    
    private lazy var barCodeImageView: UIImageView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupAddObserver()
        
        tt_isNavigationBarHidden = false
        
        view.addSubview(barCodeImageView)
        let isHidden = (tabBarController?.tabBar.isHidden ?? true) || hidesBottomBarWhenPushed
        let bottom  = isHidden ? 0 : (tabBarController?.tabBar.frame.height ?? 0)
        barCodeImageView.snp.makeConstraints { make in
            make.height.equalTo(100)
            make.left.right.equalTo(view)
            make.bottom.equalTo(-bottom)
        }
        
        view.layoutIfNeeded()
        
        let barCodeName = "SN00000000001"
        var size = CGSize.zero
        size = barCodeImageView.frame.size
        STBarCode.shared.level = .n
        STBarCode.shared.barCode(content: barCodeName, size: size) { image in
            self.barCodeImageView.image = image
        }
        
        let ad = FloatADModel()
        ad.manual_closing = true
        ad.materials = "http://cdn.startimestv.com/static/files/console/5fd275df-cc48-4986-84b3-b197979786be.gif"
        ad.show_time_duration = 10
        let placement = arc4random_uniform(3)
        ad.player_placement = STFloatADPlacement(rawValue: Int(placement)) ?? .leftBottom
        
//        view.addSubview(_playerFloatAdView)
//        _playerFloatAdView.datas = [ad]
    }
    
    private func setupAddObserver() {
        NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange(noti:)), name: UIDevice.orientationDidChangeNotification, object: nil)
    }
    
//    // 当前播放器上广告, 继承类内部也用到
//    var _playerFloatAdView = STCirculationView()
    
    var rotateDirection = DODirection.up
    // 屏幕旋转的通知
    @objc func deviceOrientationDidChange(noti:NSNotification) {
        
        //1.获取 当前设备 实例
        let deviceOrientation = UIDevice.current.orientation
        switch deviceOrientation {
        case .portrait:
            rotateDirection = .up
        case .landscapeLeft:
            rotateDirection = .left
        case .landscapeRight:
            rotateDirection = .right
        default:
            break
        }
        
       // playerFloatViewLayout()
    }
    
    private func playerFloatViewLayout() {

//        let isPortrait = rotateDirection == .up
//        // 竖屏
//        var hmargin:   CGFloat = 12
//        var bottom: CGFloat = 45
//        var width:  CGFloat = 180
//        var height: CGFloat = 72
//        
//        if !isPortrait {
//            hmargin   = 16
//            bottom = 70
//            width  = 360
//            height = 120
//        }

//        guard let ad = _playerFloatAdView.datas?.first else {
//            return
//        }
//
//        _playerFloatAdView.circulationSize = CGSize(width: width, height: height-25)
//        _playerFloatAdView.snp.remakeConstraints { make in
//            make.height.equalTo(height)
//            make.width.equalTo(width)
//            make.bottom.equalTo(self.view.snp.bottom).offset(-bottom)
//
//            if ad.player_placement == .leftBottom {
//                make.left.equalTo(hmargin)
//                return
//            }
//            if ad.player_placement == .rightBottom {
//                make.right.equalTo(self.view.snp.right).offset(-hmargin)
//                return
//            }
//            make.centerX.equalTo(self.view)
//        }
    }
}

extension BarViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("-->shantao: 2 class:\(st_className), func: \(#function)")
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        print("-->shantao: 2 class:\(st_className), func: \(#function)")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        print("-->shantao: 2 class:\(st_className), func: \(#function)")
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        print("-->shantao: 2 class:\(st_className), func: \(#function)")
    }
}
